using System;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Microsoft.SPOT.Hardware;
using System.Net;
using Microsoft.SPOT.Net.NetworkInformation;

namespace eHouse
{
    class EditIPWindow:Window
    {
        Canvas canvas = new Canvas();
        private Window editIpWindow;
        NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];

        int ip_3; 
        int ip_2;
        int ip_1;
        int ip_0;
        Text text_ip3;
        Text text_ip2;
        Text text_ip1;
        Text text_ip0;
        int gw_3;
        int gw_2;
        int gw_1;
        int gw_0;
        Text text_gw3;
        Text text_gw2;
        Text text_gw1;
        Text text_gw0;

        public EditIPWindow(Window mw)
        {
            editIpWindow = new Window();
            editIpWindow.Width = SystemMetrics.ScreenWidth;
            editIpWindow.Height = SystemMetrics.ScreenHeight;

            CreateWindow();
        }
        public void CreateWindow()
        {
            String ip = networkInterface.IPAddress.ToString();
            String[] ip_bytes = ip.Split('.');
            ip_3 = int.Parse(ip_bytes[0]);
            ip_2 = int.Parse(ip_bytes[1]);
            ip_1 = int.Parse(ip_bytes[2]);
            ip_0 = int.Parse(ip_bytes[3]);

            text_ip3 = new Text(Resources.GetFont(Resources.FontResources.NinaB), ip_3.ToString());
            text_ip2 = new Text(Resources.GetFont(Resources.FontResources.NinaB), ip_2.ToString());
            text_ip1 = new Text(Resources.GetFont(Resources.FontResources.NinaB), ip_1.ToString());
            text_ip0 = new Text(Resources.GetFont(Resources.FontResources.NinaB), ip_0.ToString());

            Panel pan_ip3 = new Panel();
            Panel pan_ip2 = new Panel();
            Panel pan_ip1 = new Panel();
            Panel pan_ip0 = new Panel();

            pan_ip3.Children.Add(text_ip3);
            pan_ip2.Children.Add(text_ip2);
            pan_ip1.Children.Add(text_ip1);
            pan_ip0.Children.Add(text_ip0);

            Image image_up_ip3 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_ip2 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_ip1 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_ip0 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));

            Image image_down_ip3 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_ip2 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_ip1 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_ip0 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));

            SimpleButton button_up_ip3 = new SimpleButton(image_up_ip3, 20, 20);
            SimpleButton button_up_ip2 = new SimpleButton(image_up_ip2, 20, 20);
            SimpleButton button_up_ip1 = new SimpleButton(image_up_ip1, 20, 20);
            SimpleButton button_up_ip0 = new SimpleButton(image_up_ip0, 20, 20);

            SimpleButton button_down_ip3 = new SimpleButton(image_down_ip3, 20, 20);
            SimpleButton button_down_ip2 = new SimpleButton(image_down_ip2, 20, 20);
            SimpleButton button_down_ip1 = new SimpleButton(image_down_ip1, 20, 20);
            SimpleButton button_down_ip0 = new SimpleButton(image_down_ip0, 20, 20);

            Canvas.SetTop(pan_ip3, 0);
            Canvas.SetLeft(pan_ip3, 0);
            canvas.Children.Add(pan_ip3);

            Canvas.SetTop(pan_ip2, 0);
            Canvas.SetLeft(pan_ip2, editIpWindow.Width/4);
            canvas.Children.Add(pan_ip2);

            Canvas.SetTop(pan_ip1, 0);
            Canvas.SetLeft(pan_ip1, 2*editIpWindow.Width / 4);
            canvas.Children.Add(pan_ip1);

            Canvas.SetTop(pan_ip0, 0);
            Canvas.SetLeft(pan_ip0, 3 * editIpWindow.Width / 4);
            canvas.Children.Add(pan_ip0);

            Canvas.SetTop(button_up_ip3, 30);
            Canvas.SetLeft(button_up_ip3, 0);
            canvas.Children.Add(button_up_ip3);

            Canvas.SetTop(button_up_ip2, 30);
            Canvas.SetLeft(button_up_ip2, editIpWindow.Width / 4);
            canvas.Children.Add(button_up_ip2);

            Canvas.SetTop(button_up_ip1, 30);
            Canvas.SetLeft(button_up_ip1, editIpWindow.Width / 2);
            canvas.Children.Add(button_up_ip1);

            Canvas.SetTop(button_up_ip0, 30);
            Canvas.SetLeft(button_up_ip0, 3 * editIpWindow.Width / 4);
            canvas.Children.Add(button_up_ip0);

            Canvas.SetTop(button_down_ip3, 60);
            Canvas.SetLeft(button_down_ip3, 0);
            canvas.Children.Add(button_down_ip3);

            Canvas.SetTop(button_down_ip2, 60);
            Canvas.SetLeft(button_down_ip2, editIpWindow.Width / 4);
            canvas.Children.Add(button_down_ip2);

            Canvas.SetTop(button_down_ip1, 60);
            Canvas.SetLeft(button_down_ip1, editIpWindow.Width / 2);
            canvas.Children.Add(button_down_ip1);

            Canvas.SetTop(button_down_ip0, 60);
            Canvas.SetLeft(button_down_ip0, 3 * editIpWindow.Width / 4);
            canvas.Children.Add(button_down_ip0);

            String gw = networkInterface.GatewayAddress.ToString();
            String[] gw_bytes = gw.Split('.');
            gw_3 = int.Parse(gw_bytes[0]);
            gw_2 = int.Parse(gw_bytes[1]);
            gw_1 = int.Parse(gw_bytes[2]);
            gw_0 = int.Parse(gw_bytes[3]);

            text_gw3 = new Text(Resources.GetFont(Resources.FontResources.NinaB), gw_3.ToString());
            text_gw2 = new Text(Resources.GetFont(Resources.FontResources.NinaB), gw_2.ToString());
            text_gw1 = new Text(Resources.GetFont(Resources.FontResources.NinaB), gw_1.ToString());
            text_gw0 = new Text(Resources.GetFont(Resources.FontResources.NinaB), gw_0.ToString());

            Panel pan_gw3 = new Panel();
            Panel pan_gw2 = new Panel();
            Panel pan_gw1 = new Panel();
            Panel pan_gw0 = new Panel();

            pan_gw3.Children.Add(text_gw3);
            pan_gw2.Children.Add(text_gw2);
            pan_gw1.Children.Add(text_gw1);
            pan_gw0.Children.Add(text_gw0);

            Image image_up_gw3 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_gw2 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_gw1 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));
            Image image_up_gw0 = new Image(Resources.GetBitmap(Resources.BitmapResources.up));

            Image image_down_gw3 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_gw2 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_gw1 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));
            Image image_down_gw0 = new Image(Resources.GetBitmap(Resources.BitmapResources.down));

            SimpleButton button_up_gw3 = new SimpleButton(image_up_gw3, 20, 20);
            SimpleButton button_up_gw2 = new SimpleButton(image_up_gw2, 20, 20);
            SimpleButton button_up_gw1 = new SimpleButton(image_up_gw1, 20, 20);
            SimpleButton button_up_gw0 = new SimpleButton(image_up_gw0, 20, 20);

            SimpleButton button_down_gw3 = new SimpleButton(image_down_gw3, 20, 20);
            SimpleButton button_down_gw2 = new SimpleButton(image_down_gw2, 20, 20);
            SimpleButton button_down_gw1 = new SimpleButton(image_down_gw1, 20, 20);
            SimpleButton button_down_gw0 = new SimpleButton(image_down_gw0, 20, 20);

            Canvas.SetTop(pan_gw3, editIpWindow.Height / 2);
            Canvas.SetLeft(pan_gw3, 0);
            canvas.Children.Add(pan_gw3);

            Canvas.SetTop(pan_gw2, editIpWindow.Height / 2);
            Canvas.SetLeft(pan_gw2, editIpWindow.Width / 4);
            canvas.Children.Add(pan_gw2);

            Canvas.SetTop(pan_gw1, editIpWindow.Height / 2);
            Canvas.SetLeft(pan_gw1, 2 * editIpWindow.Width / 4);
            canvas.Children.Add(pan_gw1);

            Canvas.SetTop(pan_gw0, editIpWindow.Height / 2);
            Canvas.SetLeft(pan_gw0, 3 * editIpWindow.Width / 4);
            canvas.Children.Add(pan_gw0);

            Canvas.SetTop(button_up_gw3, 140);
            Canvas.SetLeft(button_up_gw3, 0);
            canvas.Children.Add(button_up_gw3);

            Canvas.SetTop(button_up_gw2, 140);
            Canvas.SetLeft(button_up_gw2, editIpWindow.Width / 4);
            canvas.Children.Add(button_up_gw2);

            Canvas.SetTop(button_up_gw1, 140);
            Canvas.SetLeft(button_up_gw1, editIpWindow.Width / 2);
            canvas.Children.Add(button_up_gw1);

            Canvas.SetTop(button_up_gw0, 140);
            Canvas.SetLeft(button_up_gw0, 3 * editIpWindow.Width / 4);
            canvas.Children.Add(button_up_gw0);

            Canvas.SetTop(button_down_gw3, 170);
            Canvas.SetLeft(button_down_gw3, 0);
            canvas.Children.Add(button_down_gw3);

            Canvas.SetTop(button_down_gw2, 170);
            Canvas.SetLeft(button_down_gw2, editIpWindow.Width / 4);
            canvas.Children.Add(button_down_gw2);

            Canvas.SetTop(button_down_gw1, 170);
            Canvas.SetLeft(button_down_gw1, editIpWindow.Width / 2);
            canvas.Children.Add(button_down_gw1);

            Canvas.SetTop(button_down_gw0,170);
            Canvas.SetLeft(button_down_gw0, 3 * editIpWindow.Width / 4);
            canvas.Children.Add(button_down_gw0);


            Image image_accept = new Image(Resources.GetBitmap(Resources.BitmapResources.accept));
            image_accept.HorizontalAlignment = HorizontalAlignment.Center;
            image_accept.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_accept = new SimpleButton(image_accept, editIpWindow.Width / 2, editIpWindow.Height / 5);
            button_accept.BackColor = Colors.White;
            Canvas.SetBottom(button_accept, 0);
            Canvas.SetLeft(button_accept, 0);
            canvas.Children.Add(button_accept);

            Image image_return = new Image(Resources.GetBitmap(Resources.BitmapResources.imreturn));
            image_return.HorizontalAlignment = HorizontalAlignment.Center;
            image_return.VerticalAlignment = VerticalAlignment.Center;
            SimpleButton button_return = new SimpleButton(image_return, editIpWindow.Width / 2, editIpWindow.Height / 5);
            button_return.BackColor = Colors.White;
            Canvas.SetBottom(button_return, 0);
            Canvas.SetRight(button_return, 0);
            canvas.Children.Add(button_return);

            button_up_ip3.Click += new EventHandler(buttonUpIp3_Click);
            button_up_ip2.Click += new EventHandler(buttonUpIp2_Click);
            button_up_ip1.Click += new EventHandler(buttonUpIp1_Click);
            button_up_ip0.Click += new EventHandler(buttonUpIp0_Click);
            button_down_ip3.Click += new EventHandler(buttonDownIp3_Click);
            button_down_ip2.Click += new EventHandler(buttonDownIp2_Click);
            button_down_ip1.Click += new EventHandler(buttonDownIp1_Click);
            button_down_ip0.Click += new EventHandler(buttonDownIp0_Click);
            button_up_gw3.Click += new EventHandler(buttonUpGw3_Click);
            button_up_gw2.Click += new EventHandler(buttonUpGw2_Click);
            button_up_gw1.Click += new EventHandler(buttonUpGw1_Click);
            button_up_gw0.Click += new EventHandler(buttonUpGw0_Click);
            button_down_gw3.Click += new EventHandler(buttonDownGw3_Click);
            button_down_gw2.Click += new EventHandler(buttonDownGw2_Click);
            button_down_gw1.Click += new EventHandler(buttonDownGw1_Click);
            button_down_gw0.Click += new EventHandler(buttonDownGw0_Click);
            button_accept.Click += new EventHandler(buttonAccept_Click);
            button_return.Click += new EventHandler(buttonReturn_Click);

            editIpWindow.Child = canvas;
            editIpWindow.Visibility = Visibility.Visible;
        }

        void buttonUpIp3_Click(object sender, EventArgs evt)
        {
            ip_3++;
            if (ip_3 == 256)
                ip_3 = 0;

            text_ip3.TextContent = ip_3.ToString();
        }

        void buttonUpIp2_Click(object sender, EventArgs evt)
        {
            ip_2++;
            if (ip_2 == 256)
                ip_2 = 0;

            text_ip2.TextContent = ip_2.ToString();
        }

        void buttonUpIp1_Click(object sender, EventArgs evt)
        {
            ip_1++;
            if (ip_1 == 256)
                ip_1 = 0;

            text_ip1.TextContent = ip_1.ToString();
        }

        void buttonUpIp0_Click(object sender, EventArgs evt)
        {
            ip_0++;
            if (ip_0 == 256)
                ip_0 = 0;

            text_ip0.TextContent = ip_0.ToString();
        }

        void buttonDownIp3_Click(object sender, EventArgs evt)
        {
            ip_3--;

            if (ip_3 == -1)
                ip_3 = 255;

            text_ip3.TextContent = ip_3.ToString();
        }

        void buttonDownIp2_Click(object sender, EventArgs evt)
        {
            ip_2--;

            if (ip_2 == -1)
                ip_2 = 255;

            text_ip2.TextContent = ip_2.ToString();
        }

        void buttonDownIp1_Click(object sender, EventArgs evt)
        {
            ip_1--;

            if (ip_1 == -1)
                ip_1 = 255;

            text_ip1.TextContent = ip_1.ToString();
        }

        void buttonDownIp0_Click(object sender, EventArgs evt)
        {
            ip_0--;

            if (ip_0 == -1)
                ip_0 = 255;

            text_ip0.TextContent = ip_0.ToString();
        }

        void buttonUpGw3_Click(object sender, EventArgs evt)
        {
            gw_3++;
            if (gw_3 == 256)
                gw_3 = 0;

            text_gw3.TextContent = gw_3.ToString();
        }

        void buttonUpGw2_Click(object sender, EventArgs evt)
        {
            gw_2++;
            if (gw_2 == 256)
                gw_2 = 0;

            text_gw2.TextContent = gw_2.ToString();
        }

        void buttonUpGw1_Click(object sender, EventArgs evt)
        {
            gw_1++;
            if (gw_1 == 256)
                gw_1 = 0;

            text_gw1.TextContent = gw_1.ToString();
        }

        void buttonUpGw0_Click(object sender, EventArgs evt)
        {
            gw_0++;
            if (gw_0 == 256)
                gw_0 = 0;

            text_gw0.TextContent = gw_0.ToString();
        }

        void buttonDownGw3_Click(object sender, EventArgs evt)
        {
            gw_3--;

            if (gw_3 == -1)
                gw_3 = 255;

            text_gw3.TextContent = gw_3.ToString();
        }

        void buttonDownGw2_Click(object sender, EventArgs evt)
        {
            gw_2--;

            if (gw_2 == -1)
                gw_2 = 255;

            text_gw2.TextContent = gw_2.ToString();
        }

        void buttonDownGw1_Click(object sender, EventArgs evt)
        {
            gw_1--;

            if (gw_1 == -1)
                gw_1 = 255;

            text_gw1.TextContent = gw_1.ToString();
        }

        void buttonDownGw0_Click(object sender, EventArgs evt)
        {
            gw_0--;

            if (gw_0 == -1)
                gw_0 = 255;

            text_gw0.TextContent = gw_0.ToString();
        }
        void buttonAccept_Click(object sender, EventArgs evt)
        {
            string ip = ip_3 + "." + ip_2 + "." + ip_1 + "." + ip_0;  
            string gateway = gw_3 + "." + gw_2 + "." + gw_1 + "." + gw_0;
            networkInterface.EnableStaticIP(ip, "255.255.255.0", gateway);
        }

        void buttonReturn_Click(object sender, EventArgs evt)
        {
            editIpWindow.Close();
        }
    
    }
}
